package com.smooware.icms.adapter.pager;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.smooware.icms.R;
import com.smooware.icms.adapter.list.SheduleListAdapter;
import com.smooware.icms.adapter.list.WOInventoryListAdapter;
import com.smooware.icms.adapter.list.WOKeyListAdapter;
import com.smooware.icms.data.model.Inventory;
import com.smooware.icms.data.model.Key;
import com.smooware.icms.data.model.WorkOrder;
import com.smooware.icms.data.model.util.WOJobType;
import com.smooware.icms.data.model.util.WOSheduleType;
import com.smooware.icms.data.model.util.WOStatus;

public class WOShedulesPagerAdapter extends PagerAdapter {

	private static final int KEY_PAGE_POSITION = 2;

	private static final int INVENTORY_PAGE_POSITION = 1;

	private static final int WO_PAGE_POSITION = 0;

	private static final String TAG = WOShedulesPagerAdapter.class
			.getSimpleName();

	private static final int NUM_VIEWS = 3;

	private final Map<Integer, View> pages = new HashMap<Integer, View>();

	private final Context context;

	public WOShedulesPagerAdapter(final Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		return NUM_VIEWS;
	}

	@Override
	public Object instantiateItem(final View collection, final int position) {
		Log.d(TAG, "instantiateItem " + position);

		final View view;

		if (pages.containsKey(position)) {
			Log.d(TAG, "from map " + position);

			view = pages.get(position);
		} else {
			Log.d(TAG, "creates new " + position);

			view = createPageForPosition(position);

			pages.put(position, view);
		}

		((ViewGroup) collection).addView(view);

		return view;
	}

	@Override
	public void destroyItem(final View collection, final int position,
			final Object view) {
		Log.d(TAG, "destroyItem " + position);

		((ViewGroup) collection).removeView((View) view);
	}

	@Override
	public boolean isViewFromObject(final View view, final Object object) {
		return view == ((View) object);
	}

	private View createPageForPosition(final int position) {
		final View view = View.inflate(context,
				R.layout.shedule_faragment_page, null);

		ListView items = (ListView) view.findViewById(R.id.shedule_list_items);

		switch (position) {
		case WO_PAGE_POSITION:
			items.setAdapter(createWOPageAdapter());
			break;
		case INVENTORY_PAGE_POSITION:
			items.setAdapter(createInventoryPageAdapter());
			break;
		case KEY_PAGE_POSITION:
			items.setAdapter(createKeyPageAdapter());
			break;
		default:
			items.setAdapter(createWOPageAdapter());
			break;
		}
		return view;
	}

	private SheduleListAdapter createWOPageAdapter() {
		SheduleListAdapter adapter = new SheduleListAdapter(context);

		for (int i = 0; i < 25; i++) {
			WorkOrder workOrder = new WorkOrder();

			workOrder.setAddress("Some demo street, 12");
			workOrder
					.setShortDesription("this is some pretty short description");
			workOrder.setJobType(WOJobType.PREVENTIVE);
			workOrder.setSheduleType(WOSheduleType.RESIDENT_REQUEST);
			workOrder.setStatus(WOStatus.OPEN);

			adapter.add(workOrder);

			workOrder = new WorkOrder();

			workOrder.setAddress("Some demo street, 12");
			workOrder
					.setShortDesription("this is some pretty short description");
			workOrder.setJobType(WOJobType.PREVENTIVE);
			workOrder.setSheduleType(WOSheduleType.PLANNED);
			workOrder.setStatus(WOStatus.CLOSED);

			adapter.add(workOrder);
		}
		return adapter;
	}

	private WOInventoryListAdapter createInventoryPageAdapter() {

		WOInventoryListAdapter adapter = new WOInventoryListAdapter(context);

		for (int i = 0; i < 25; i++) {
			Inventory inventory = new Inventory();

			inventory.setIcmsItemNumber(1234);
			inventory.setCount(12);
			inventory.setItemDescription("something interesting");
			inventory.setItemSKU("16-14700");
			inventory.setItemUPC("16-14700");

			adapter.add(inventory);
		}
		return adapter;
	}

	private WOKeyListAdapter createKeyPageAdapter() {
		WOKeyListAdapter adapter = new WOKeyListAdapter(context);

		for (int i = 0; i < 25; i++) {
			Key key = new Key();

			key.setKeyNumber(5074);
			key.setKeyLocation("865762, Bldg. 10 Unit 1002");
			key.setKeyStatus("Taken on 25/06 by you");
			key.setHasKey(true);

			adapter.add(key);
		}
		return adapter;
	}
}
